//
//  CCEditorAction.m
//  Cocos2DEditor
//
//  Created by waver on 10-6-21.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "CCEditorAction.h"
#import "Role.h"
#import "Frame.h"
#import "Action.h"



@implementation CCEditorAction

@synthesize __imageNameArray,__AnimationArray;

-(id)InitAction:(NSString* )fileName
{
	if(__currentRole != nil)
	{
		delete ((Role*)__currentRole);
		__currentRole = nil;
	}
	
	__currentRole = new Role([fileName UTF8String]);
	
	[self AnalyseFrame];
	
	return self;
}


-(void)InitArray
{
	
	if(nil == __imageNameArray)
	{
		__imageNameArray = [[NSMutableArray alloc]init];
	}
	else
	{
		[__imageNameArray release];
		__imageNameArray = [[NSMutableArray alloc] init];
	}
	
	if(nil == __AnimationArray)
	{
		__AnimationArray = [[NSMutableArray alloc] init];
	}
	else
	{
		[__AnimationArray release];
		__AnimationArray = [[NSMutableArray alloc] init];
	}
	
	if(nil == __FrameArray)
	{
		__FrameArray = [[NSMutableArray alloc] init];
	}
	else
	{
		[__FrameArray release];
		__FrameArray = [[NSMutableArray alloc] init];
	}
	
	if(nil == __timeArray)
	{
		__timeArray = [[NSMutableArray alloc] init];
	}
	else
	{
		[__timeArray release];
		__timeArray = [[NSMutableArray alloc] init];
	}
	
}

-(void)AnalyseFrame
{
	vector<char*>& nameVec = ((Role*)__currentRole)->getImageName();
	
	[self InitArray];
	
	//stroe the image name
	for(unsigned int i=0; i<nameVec.size();i++)
	{
		const char* nameP = nameVec[i];
		NSString * nameStr = [NSString stringWithCString:nameP];
		[__imageNameArray addObject:nameStr];
		[nameStr retain];
	}
	
	vector<Action*>&ActionVec  = ((Role*)__currentRole)->GetActionVec();
	for(unsigned int i=0;i < ActionVec.size();i++)
	{
		vector<Frame*> & FrameVec = ActionVec[i]->GetActionFrame();
		
		NSNumber * pNum = [NSNumber numberWithInt:i];
		
		NSMutableArray* animFrames = [NSMutableArray array];
		
		float m_iTime;
		
		int x_one_center = 0;
		int y_one_center = 0;
		
		for(unsigned int i=0;i < FrameVec.size();i++)
		{
			Frame* currentFrame = FrameVec[i] ;
			
			//int elementNum = (int)(currentFrame->m_Num);
			
			int	dis_x = *(currentFrame->m_iClipX);
			int dis_y = *(currentFrame->m_iClipY);
			
			int	width = *(currentFrame->m_iClipW);
			int height = *(currentFrame->m_iClipH);
			
			int offset_x = *(currentFrame->m_iX);
			int offset_y = *(currentFrame->m_iY);

			
			CGPoint realCenter;
			
			CGPoint mirCenter;
			
			realCenter.x = width/2;
			realCenter.y = height/2;
			
			int offsetY = -(offset_y)-height;
			
			
			byte imageIndex = *(currentFrame->imgID);
			
			//image elemet offset display to the zero
			//CGPoint offsetElement = CGPointMake(-1*(*(currentFrame->m_iX))-width/2,-1*offsetY-height/2);
			//CGPoint offsetElement = CGPointMake(-1*(*(currentFrame->m_iX)),-1*offsetY);
			CGPoint offsetElement = CGPointMake(0,0);
			
			/*
			if(i ==0)
			{
				x_one_center = offset_x+width/2;
				y_one_center = -offset_y-height/2;
				offsetElement.x = 0;
				offsetElement.y = 0;
			}
			else
			{
				offsetElement.x = (offset_x+width/2) - x_one_center;
				offsetElement.y = (-offset_y-height/2)-y_one_center;
			}
			 */
			if(i ==0)
			{
				x_one_center = offset_x+width/2;
				y_one_center = -offset_y-height/2;
				offsetElement.x = 0;
				offsetElement.y = height/2;
			}
			else
			{
				offsetElement.x = offset_x+width/2-x_one_center;
				offsetElement.y = -offset_y-y_one_center;
			}

			//CGPoint offsetElement = CGPointMake(*(currentFrame->m_iX),offsetY);
			//image rect
			CGRect	imageRect = CGRectMake(dis_x, dis_y, width, height);
			
			CCTexture2D* pTexture = [[CCTextureCache sharedTextureCache]addImage:[__imageNameArray objectAtIndex:imageIndex]];
			
			//create frame add to animation
			CCSpriteFrame * addFrame = [CCSpriteFrame frameWithTexture:pTexture rect:imageRect offset:offsetElement];
			m_iTime = currentFrame->m_iTime;
			[animFrames addObject:addFrame];
		}
		
		NSNumber * pNumTime  = [NSNumber numberWithFloat:m_iTime];
		
		[__timeArray addObject:pNumTime];
		
		
		CCAnimation * pCCAnimation = [CCAnimation animationWithName:[pNum stringValue] delay:m_iTime frames:animFrames];
		
		[__FrameArray addObject:animFrames];
		
		//[animFrames retain];
		
		[pCCAnimation retain];
		
		[__AnimationArray addObject:pCCAnimation];
	}
	
}



-(CCAnimation*)AnimationInEditor:(int)aniIndex doubleTime:(int)times
{
	if(((unsigned int)aniIndex) >([__AnimationArray count]-1))
	{
		return NULL;
	}
	else
	{
		NSArray * FrameArray = [[NSArray alloc] initWithArray:[__FrameArray objectAtIndex:aniIndex]];
		
		//frame speed 
		float m_iTime  = [[__timeArray objectAtIndex:aniIndex] floatValue];
		if(m_iTime == 0)
		{
			m_iTime  = 1000;
		}
		if(m_iTime < 150)
		{
			m_iTime = 150;
		}
		m_iTime = (m_iTime/1000.0) * ((float)times);
		m_iTime = m_iTime;
		//animation name
		NSNumber * pNum = [NSNumber numberWithInt:aniIndex];
		CCAnimation * pCCAnimation = [CCAnimation animationWithName:[pNum stringValue] delay:m_iTime frames:FrameArray];
		[pCCAnimation retain];
		
		[FrameArray release];
		
		return pCCAnimation;
	}
	
}

@end
